/**
 *数据库工具类包
 */
package com.sjtu.database;

import java.sql.*;

/**
 *
 * @author Eason
 */
public class DataBase {

    static String mySqlDriver = "com.mysql.jdbc.Driver";
    static String url = "jdbc:mysql://localhost:3306/mslibrary?useUnicode=true&characterEncoding=utf8";
    static String username = "root";
    static String userpass = "";
    private Connection conn;
    private Statement s;
    private static DataBase database;

    public static DataBase getInstance() {
    	if (database == null)
    		database = new DataBase();
    	return database;
    }

    private DataBase() {
    	try {
            Class.forName(mySqlDriver).newInstance();
            conn = DriverManager.getConnection(url, username, userpass);
            s = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    public ResultSet query(String sql) {
    	try {
    		return s.executeQuery(sql);
    	} catch (SQLException e) {
    		System.out.println("Error occured in SQL: "+sql);
    		return null;
    	}
    }

    public boolean update(String sql) {
    	try {
            s.executeUpdate(sql);
    		return true;
    	} catch (SQLException e) {
    		System.out.println("Error occured in SQL: "+sql);
    		return false;
    	}
    }
}
